<!--开始任务-->
<template>
  <div class="app-container">
    <!--表单模块-->
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span class="el-icon-document">表单信息</span>
        <el-button style="float: right;" type="primary" @click="goBack">返回</el-button>
      </div>
      <!--初始化流程加载表单信息-->
      <el-col :span="16" :offset="4">
        <div class="test-form">
          <parser :key="new Date().getTime()" :form-conf="formConf" @submit="submitForm"/>
        </div>
      </el-col>
    </el-card>

    <!--流程图-->
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span class="el-icon-picture-outline">流程图</span>
      </div>
      <flow :xmlData="xmlData" :taskData="taskList"></flow>
    </el-card>
  </div>
</template>

<script>
import {getFlowForm} from "@/api/flowable/finished";
import Parser from '@/components/parser/Parser'
import {definitionStart, readXml} from "@/api/flowable/definition";
import flow from '@/views/flowable/task/record/flow'

export default {
  name: "StartTask",
  components: {
    'parser': Parser,
    'flow': flow
  },
  props: {},
  data() {
    return {
      deployId: null,   // 流程定义编号
      procDefId: null,  // 流程定义编号

      formConf: {}, // 默认表单数据

      xmlData: "",
      taskList: []
    };
  },
  created() {

  },
  mounted() {
    this.deployId = this.$route.query && this.$route.query.deployId;
    this.procDefId = this.$route.query && this.$route.query.procDefId;

    //获取流程表单数据
    this.getFlowFrm(this.deployId);

    //画流程图
    this.getModelDetail(this.deployId);
  },
  methods: {
    /** xml 文件 */
    getModelDetail(deployId) {
      // 发送请求，获取xml
      readXml(deployId).then(res => {
        this.xmlData = res.data;
      }).catch(res => {

      });
    },
    /**流程表单**/
    getFlowFrm(deployId) {
      if (deployId == null) {
        return;
      }
      const params = {deployId: deployId};
      getFlowForm(params).then(res => {
        this.formConf = res.data;
      }).catch(res => {
        this.goBack();
      });
    },
    /** 返回页面 */
    goBack() {
      // 关闭当前标签页并返回上个页面
      this.$store.dispatch("tagsView/delView", this.$route);
      this.$router.go(-1);
    },
    /** 申请流程表单数据提交 */
    submitForm(data) {
      if (data) {
        const valData = data.valData;
        const formData = data.formData;
        formData.disabled = true;
        formData.formBtns = false;
        if (this.procDefId) {
          valData.variables = formData;
           // 启动流程并将表单数据加入流程变量
          definitionStart(this.procDefId, JSON.stringify(valData)).then(res => {
            this.msgSuccess(res.msg);
            this.goBack();
          }).catch(() => {

          });
        }
      }
    }
  }
};
</script>
<style lang="scss" scoped>
.test-form {
  margin: 15px auto;
  width: 800px;
  padding: 15px;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}
.clearfix:after {
  clear: both
}

.box-card {
  width: 100%;
  margin-bottom: 20px;
}

.el-tag + .el-tag {
  margin-left: 10px;
}
</style>
